home *** CD-ROM | disk | FTP | other *** search
/ Hardcore Visual Basic 5.0 (2nd Edition) / Hardcore Visual Basic 5.0 - Second Edition (1997)(Microsoft Press).iso / Code / timeit.frm < prev    next >
Text File  |  1997-06-14  |  13KB  |  385 lines

  1. VERSION 5.00
  2. Begin VB.Form FTimeIt 
  3.    BackColor       =   &H8000000A&
  4.    Caption         =   "Time It"
  5.    ClientHeight    =   5745
  6.    ClientLeft      =   1260
  7.    ClientTop       =   1665
  8.    ClientWidth     =   9120
  9.    BeginProperty Font 
  10.       Name            =   "MS Sans Serif"
  11.       Size            =   8.25
  12.       Charset         =   0
  13.       Weight          =   700
  14.       Underline       =   0   'False
  15.       Italic          =   0   'False
  16.       Strikethrough   =   0   'False
  17.    EndProperty
  18.    Icon            =   "timeit.frx":0000
  19.    LinkTopic       =   "Form1"
  20.    ScaleHeight     =   5745
  21.    ScaleWidth      =   9120
  22.    Begin VB.TextBox txtIteration 
  23.       Height          =   495
  24.       Left            =   105
  25.       TabIndex        =   5
  26.       Top             =   1665
  27.       Width           =   1575
  28.    End
  29.    Begin VB.ListBox lstProblems 
  30.       Height          =   2595
  31.       Left            =   108
  32.       TabIndex        =   3
  33.       Top             =   2595
  34.       Width           =   3135
  35.    End
  36.    Begin VB.CommandButton cmdTimeIt 
  37.       Caption         =   "&Time It"
  38.       Default         =   -1  'True
  39.       Height          =   495
  40.       Left            =   150
  41.       TabIndex        =   2
  42.       Top             =   105
  43.       Width           =   1215
  44.    End
  45.    Begin VB.CommandButton cmdExit 
  46.       Cancel          =   -1  'True
  47.       Caption         =   "E&xit"
  48.       Height          =   495
  49.       Left            =   150
  50.       TabIndex        =   0
  51.       Top             =   705
  52.       Width           =   1215
  53.    End
  54.    Begin VB.Label Label2 
  55.       Caption         =   "Problems:"
  56.       Height          =   255
  57.       Left            =   105
  58.       TabIndex        =   7
  59.       Top             =   2265
  60.       Width           =   1215
  61.    End
  62.    Begin VB.Label Label1 
  63.       Caption         =   "Iterations:"
  64.       Height          =   255
  65.       Left            =   105
  66.       TabIndex        =   6
  67.       Top             =   1425
  68.       Width           =   1215
  69.    End
  70.    Begin VB.Label lblDescription 
  71.       Height          =   648
  72.       Left            =   3408
  73.       TabIndex        =   4
  74.       Top             =   120
  75.       UseMnemonic     =   0   'False
  76.       Width           =   5484
  77.    End
  78.    Begin VB.Label lblOutput 
  79.       Height          =   4812
  80.       Left            =   3384
  81.       TabIndex        =   1
  82.       Top             =   840
  83.       Width           =   5604
  84.    End
  85. End
  86. Attribute VB_Name = "FTimeIt"
  87. Attribute VB_GlobalNameSpace = False
  88. Attribute VB_Creatable = False
  89. Attribute VB_PredeclaredId = True
  90. Attribute VB_Exposed = False
  91. Option Explicit
  92.  
  93. Private problems As New Collection
  94. Private problem As CProblem
  95.  
  96. Const sLogicalAndVsNestedIf = "Logical And Versus Nested If"
  97. Const sByValVsByRef = "By Value Versus By Reference"
  98. Const sCompareTypeProcessing = "Processing Different Types"
  99. Const sInlineVsFunction = "Inline Versus Procedure"
  100. Const sDeclareVsTypeLib = "Declare Versus Type Library"
  101. Const sFixedVsVariableString = "Fixed Versus Variable String"
  102. Const sCompareLoWords = "LoWord Variations"
  103. Const sCShiftVsBShift = "Shift Versus Multiply or Divide"
  104. Const sIIfVsIfThen = "IIf Versus If/Then/Else"
  105. Const sDollarVsNone = "String Versus Variant Functions"
  106. Const sEmptyVsQuotes = "Empty Versus Blank String"
  107. Const sWithWithout = "Object Access Using With"
  108. Const sMethodVsProc = "Methods Versus Procedures"
  109. Const sForEachVsForI = "For Each Versus For Index"
  110. Const sSortCollectVsArray = "Shuffle, Sort, and Search"
  111. Const sAddCollect = "Add To Collection"
  112. Const sSortRecurseVsIterate = "Recursive Versus Iterative Sorting"
  113. Const sSortNameVsSortPoly = "Compare Sorting Methods"
  114. Const sCompareFindFiles = "Compare File Finding Methods"
  115. Const sCompareExistFile = "Compare File Existence Tests"
  116. Const sFriendVsPublic = "Friend Properties Versus Public Properties"
  117.  
  118. Private Sub Form_Load()
  119.    
  120.     Dim f As Boolean
  121.     ChDir CurDir$
  122.     txtIteration.Locked = False
  123.           
  124.     Set problem = New CProblem
  125.     problem.Title = sLogicalAndVsNestedIf
  126.     problem.Description = _
  127.         "Does Basic short-circuit logical expressions? " & _
  128.         "Or does a logical And operation take the " & _
  129.         "same time as an equivalent nested If/Then?"
  130.     problem.Iterations = 300000
  131.     problems.Add problem, problem.Title
  132.  
  133.     Set problem = New CProblem
  134.     problem.Title = sByValVsByRef
  135.     problem.Description = _
  136.         "How does the timing of ByVal arguments compare " & _
  137.         "to the timing of the default By Reference arguments?"
  138.     problem.Iterations = 60000
  139.     problems.Add problem, problem.Title
  140.  
  141.     Set problem = New CProblem
  142.     problem.Description = _
  143.         "Compare counting with different types: Integer, " & _
  144.         "Long, Single, Double, Currency, and Variant"
  145.     problem.Title = sCompareTypeProcessing
  146.     problem.Iterations = 500
  147.     problems.Add problem, problem.Title
  148.  
  149.     Set problem = New CProblem
  150.     problem.Title = sInlineVsFunction
  151.     problem.Description = _
  152.         "Compare an operation inline with the same " & _
  153.         "operation in a function."
  154.     problem.Iterations = 30000
  155.     problems.Add problem, problem.Title
  156.  
  157.     Set problem = New CProblem
  158.     problem.Title = sDeclareVsTypeLib
  159.     problem.Description = _
  160.         "Compare calling an API function through a Declare " & _
  161.         "statement to calling through a type library."
  162.     problem.Iterations = 30000
  163.     problems.Add problem, problem.Title
  164.  
  165.     Set problem = New CProblem
  166.     problem.Title = sFixedVsVariableString
  167.     problem.Description = _
  168.         "Compare various operations on variable-length strings with the same " & _
  169.         "operations on fixed-length strings."
  170.     problem.Iterations = 30000
  171.     problems.Add problem, problem.Title
  172.  
  173.     Set problem = New CProblem
  174.     problem.Title = sCompareLoWords
  175.     problem.Description = _
  176.         "Compare different methods of getting the low " & _
  177.         "word of a long without getting overflow errors"
  178.     problem.Iterations = 5000
  179.     problems.Add problem, problem.Title
  180.  
  181.     Set problem = New CProblem
  182.     problem.Title = sIIfVsIfThen
  183.     problem.Description = _
  184.         "Is IIf faster than an equivalent If/Then/Else block?"
  185.     problem.Iterations = 30000
  186.     problems.Add problem, problem.Title
  187.  
  188.     Set problem = New CProblem
  189.     problem.Title = sDollarVsNone
  190.     problem.Description = _
  191.         "Do Mid and Mid$ do exactly the same thing?"
  192.     problem.Iterations = 300
  193.     problems.Add problem, problem.Title
  194.  
  195.     Set problem = New CProblem
  196.     problem.Title = sEmptyVsQuotes
  197.     problem.Description = _
  198.         "Compare various forms of empty strings including " & _
  199.         "double quotes, sEmpty, Empty, and vbNullString."
  200.     problem.Iterations = 10000
  201.     problems.Add problem, problem.Title
  202.  
  203.     Set problem = New CProblem
  204.     problem.Title = sWithWithout
  205.     problem.Description = _
  206.         "Compare object access using With to equivalent " & _
  207.         "fully-specified object access when used both with " & _
  208.         "object variables and reference object variables"
  209.     problem.Iterations = 3000
  210.     problems.Add problem, problem.Title
  211.  
  212.     Set problem = New CProblem
  213.     problem.Title = sMethodVsProc
  214.     problem.Description = _
  215.         "Compare direct and indirect (using Set) object operations " & _
  216.         "to comparable operations using procedures and variables."
  217.     problem.Iterations = 5000
  218.     problems.Add problem, problem.Title
  219.  
  220.     Set problem = New CProblem
  221.     problem.Title = sForEachVsForI
  222.     problem.Description = _
  223.         "Compare iterating through various collections and arrays " & _
  224.         "with For Each to iterating through with For using an index " & _
  225.         "variable."
  226.     problem.Iterations = 60
  227.     problems.Add problem, problem.Title
  228.  
  229.     Set problem = New CProblem
  230.     problem.Title = sSortCollectVsArray
  231.     problem.Description = _
  232.         "Compare filling, shuffling, sorting, and searching an " & _
  233.         "array with the same operations on a collection."
  234.     problem.Iterations = 300
  235.     problems.Add problem, problem.Title
  236.  
  237.     Set problem = New CProblem
  238.     problem.Title = sAddCollect
  239.     problem.Description = _
  240.         "Compare adding new items to a collection at the " & _
  241.         "beginning, middle, and end"
  242.     problem.Iterations = 8000
  243.     problems.Add problem, problem.Title
  244.     
  245.     Set problem = New CProblem
  246.     problem.Title = sSortRecurseVsIterate
  247.     problem.Description = _
  248.         "Compare sorting recursively with sorting iteratively."
  249.     problem.Iterations = 200
  250.     problems.Add problem, problem.Title
  251.     
  252.     Set problem = New CProblem
  253.     problem.Title = sSortNameVsSortPoly
  254.     problem.Description = _
  255.         "Compare faking procedure variables with a name-space " & _
  256.         "hack to faking them with a polymorphic class."
  257.     problem.Iterations = 200
  258.     problems.Add problem, problem.Title
  259.     
  260.     Set problem = New CProblem
  261.     problem.Title = sCompareFindFiles
  262.     problem.Description = _
  263.         "Compare finding files with Dir$ and with the " & _
  264.         "FindFirstFiles API function."
  265.     problem.Iterations = 1
  266.     problems.Add problem, problem.Title
  267.  
  268.     Set problem = New CProblem
  269.     problem.Title = sCompareExistFile
  270.     problem.Description = _
  271.         "Compare different ways to test for the existence of a file."
  272.     problem.Iterations = 500
  273.     problems.Add problem, problem.Title
  274.     
  275.     Set problem = New CProblem
  276.     problem.Description = _
  277.         "Compare calling Friend properties on classes and forms with " & _
  278.         "calling equivalent Public properties on classes and forms."
  279.     problem.Title = sFriendVsPublic
  280.     problem.Iterations = 100000
  281.     problems.Add problem, problem.Title
  282.  
  283.     For Each problem In problems
  284.         lstProblems.AddItem problem.Title
  285.     Next
  286.     lstProblems.ListIndex = 0
  287.  
  288. End Sub
  289.  
  290. Private Sub cmdTimeIt_Click()
  291.     HourGlass Me
  292.     lblOutput.Caption = "Working..."
  293.     DoEvents
  294.     Dim s As String, cIter As Long
  295.     cIter = problem.Iterations
  296.     Select Case problem.Title
  297.     Case sLogicalAndVsNestedIf
  298.         s = LogicalAndVsNestedIf(cIter)
  299.     Case sByValVsByRef
  300.         s = ByValVsByRef(cIter)
  301.     Case sCompareTypeProcessing
  302.         s = CompareTypeProcessing(cIter)
  303.     Case sInlineVsFunction
  304.         s = InlineVsFunction(cIter)
  305.     Case sDeclareVsTypeLib
  306.         s = DeclareVsTypeLib(cIter)
  307.     Case sFixedVsVariableString
  308.         s = FixedVsVariableString(cIter)
  309.     Case sCompareLoWords
  310.         s = CompareLoWords(cIter)
  311.     Case sIIfVsIfThen
  312.         s = IIfVsIfThen(cIter)
  313.     Case sDollarVsNone
  314.         s = DollarVsNone(cIter)
  315.     Case sEmptyVsQuotes
  316.         s = EmptyVsQuotes(cIter)
  317.     Case sWithWithout
  318.         s = WithWithout(cIter)
  319.     Case sMethodVsProc
  320.         s = MethodVsProc(cIter)
  321.     Case sForEachVsForI
  322.         s = ForEachVsForI(cIter)
  323.     Case sSortCollectVsArray
  324.         s = SortCollectVsArray(cIter)
  325.     Case sAddCollect
  326.         s = AddCollect(cIter)
  327.     Case sSortRecurseVsIterate
  328.         s = SortRecurseVsIterate(cIter)
  329.     Case sSortNameVsSortPoly
  330.         s = SortNameVsSortPoly(cIter)
  331.     Case sCompareFindFiles
  332.         s = CompareFindFiles(cIter)
  333.     Case sCompareExistFile
  334.         s = CompareExistFile(cIter)
  335.     Case sFriendVsPublic
  336.         s = CompareFriendVsPublic(cIter)
  337.     End Select
  338.     HourGlass Me
  339.     BugMessage problem.Description
  340.     BugMessage "Iterations: " & problem.Iterations
  341.     BugMessage s
  342.     lblOutput.Caption = s
  343. End Sub
  344.  
  345. Private Sub cmdExit_Click()
  346.     Unload Me
  347. End Sub
  348.  
  349. Private Sub lstProblems_DblClick()
  350.     cmdTimeIt_Click
  351. End Sub
  352.  
  353.  
  354. Private Sub txtIteration_Change()
  355.     Dim i As Long
  356.     i = Val(txtIteration.Text)
  357.     If i Then
  358.         problem.Iterations = i
  359.     Else
  360.         Beep
  361.         txtIteration.Text = problem.Iterations
  362.     End If
  363. End Sub
  364.  
  365. Private Sub lstProblems_Click()
  366.     Dim s As String
  367.     lblOutput = sEmpty
  368.     s = lstProblems.List(lstProblems.ListIndex)
  369.     Set problem = problems(s)
  370.     lblDescription.Caption = problem.Description
  371.     txtIteration.Text = problem.Iterations
  372.     'txtIteration.SetFocus
  373.     txtIteration.SelStart = 0
  374.     txtIteration.SelLength = Len(txtIteration.Text)
  375. End Sub
  376.  
  377. Public Property Get ProcProp() As Long
  378.     ProcProp = 1
  379. End Property
  380.  
  381. Friend Property Get FriendProp() As Long
  382.     FriendProp = 1
  383. End Property
  384.  
  385.